<template>
  <AddOrEditDialog
    ref="addOrEditDialogRef"
    :form="form"
    title="教学班"
    :formHeader="formHeader"
    :url="$urls.API_URL_BACKSTAGE_SCHOOLTEACHINGCLASSSTUDENT_MANAGE"
    :action="action"
    @afterSubmitSuccess="afterSubmitSuccess"
    >
    <template #parentId>
      <SysDepartmentTreeSelect
        v-model="form['parentId']"
        :data="table.data"
        clearable/>
    </template>
  </AddOrEditDialog>
</template>

<script lang="ts" setup>
// 引入添加编辑模块
import AddOrEditDialog from '@/components/base/table/form/AddOrEditDialog.vue';
import {
  computed,
  defineExpose, defineProps, getCurrentInstance, reactive, ref, withDefaults,
} from 'vue';
// 引入表格相关
import { TableColumn, FormHeader, TableData } from '@/types/interface/base_interface';
// 引入SchoolTeachingClass对象,教学班
import { SchoolTeachingTaskTeacher } from '@/types/interface/school_interface';

const { proxy } = getCurrentInstance();

// 接收父容器参数
interface Props {
  table: TableData, // 偷懒写法
  action?: string, // 表单的处理类型，post表示添加，put表示编辑
}

const props = withDefaults(defineProps<Props>(), {
  action: 'post',
});

// 定义表单元素
const formHeader = ref<FormHeader[]>([
  {
    label: '教学班级',
    prop: 'teachingClassId',
    rules: [
      {
        required: true,
        message: '教学班级不能为空',
        trigger: 'blur',
      },
    ],
  },
  {
    label: '学生',
    prop: 'studentId',
    rules: [
      {
        required: true,
        message: '学生不能为空',
        trigger: 'blur',
      },
    ],
  },
  {
    label: '类型（1、主讲，2、助教）',
    prop: 'type',
    type: 'number',
    rules: [
      {
        required: true,
        message: '类型不能为空',
        trigger: 'blur',
      },
    ],
  },
]);
// 表单对象
const form = ref<SchoolTeachingTaskTeacher>();
const addOrEditDialogRef = ref(); // 表单ref对象
let formInit:SchoolTeachingTaskTeacher; // 记录表单第一次传入的值,用于表单重置恢复初始值使用
// 打开对话框，供父组件调用，传入表单对象
const openDialog = (schoolTeachingTaskTeacher: SchoolTeachingTaskTeacher) => {
  formInit = { ...schoolTeachingTaskTeacher }; // 获取初始值，这里要做深拷贝，避免与form对象引用的地址一致，导致无法恢复初始值
  form.value = schoolTeachingTaskTeacher;
  // 打开对话框
  addOrEditDialogRef.value!.openDialog();
};
// 提交表单成功后的钩子函数
const afterSubmitSuccess = () => {
  // 让表单恢复为传入对象时的初始值，注意要深度拷贝，避免初始值收到污染
};
// 暴露组件给父容器使用
defineExpose({
  openDialog,
});
</script>
<style lang="scss" scoped>

</style>
